package com.woniuxy.store.app.schedules;

import com.woniuxy.store.service.StoreInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

@Component
public class ScheduleTask implements SchedulingConfigurer {
    @Value("${schedules.storexpiretask.cron}")
    private String storExpireTask;

    @Autowired
    private StoreInfoService storeInfoService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        System.out.println(storExpireTask);
        taskRegistrar.addTriggerTask(()-> {
            try {
                storeInfoService.checkStoreExpire();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, triggerContext -> {
            CronTrigger cronTrigger = new CronTrigger(storExpireTask);
            return cronTrigger.nextExecutionTime(triggerContext);
        });

    }

    public void setChangeSaleTask(String storExpireTask) {
        this.storExpireTask = storExpireTask;
    }


}
